
package com.wodan.zhongjinjicang.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wodan.zhongjinjicang.common.exception.BusinessException;
import com.wodan.zhongjinjicang.common.utils.JsonUtils;
import com.wodan.zhongjinjicang.enums.BusinessErrorEnum;
import com.wodan.zhongjinjicang.frame.AbstractClientApiJsonBusinessMethodService;
import com.wodan.zhongjinjicang.mapper.UserMapper;
import com.wodan.zhongjinjicang.model.UserBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


/**
 * 修改用户基本信息
 */
@Service("editProfile")
public class UserEditProfileService extends AbstractClientApiJsonBusinessMethodService {


    private static final String USER_ID = "id";


    @Autowired
    private UserMapper userDao;


    @Override
    protected JSONObject doExecute(JSONObject data, String sessionId) {
        UserBean user = JSON.parseObject(JSON.toJSONString(data),UserBean.class);

        int i = userDao.updateProfileById(user);
        if(i>0){
            Map<String , Object> modelMap = new HashMap<String , Object>();
            modelMap.put("userInfo", user);
            return new JSONObject(modelMap);
        }else{
            throw new BusinessException(BusinessErrorEnum.ERROR.getCode(), "修改失败");
        }

    }


	@Override
	protected boolean isNeedLogin() {
		return true;
	}

    @Override
    protected String[] getRequiredParams() {
        return new String[]{USER_ID};
    }

}
